/* eslint valid-jsdoc: "off" */

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1715052892660_1521';

  // add your middleware config here
  config.middleware = [];
    // 异常处理
    config.onerror = {
        accepts(){
            return  'json'
        },
        json(err,ctx){
            console.log(err);
            // 自定义错误时的响应体
            if(err.status === 422){
                // 判断必传
                if(err.errors[0].message == 'required'){
                    ctx.body = {
                        msg: '缺少必传参数',
                        field: err.errors[0].field
                    }
                    ctx.status = 400
                }else{
                    ctx.body = {
                        msg: err.errors[0].message,
                        field: err.errors[0].field
                    }
                    ctx.status = 422
                }
            }else{
                ctx.body = {
                    msg: err.message,
                    ...(err.errors  && {errors:err.errors})
                }
                ctx.status = err.status

            }
        }
    }
//   连接数据库
config.mongoose = {
    url: 'mongodb://127.0.0.1/chabaidao-project',
}
// 取消安全威胁csrf防范
config.security = {
    csrf:{
        enable:false
    }
}
// 校验插件配置
validate = {
    // 
    convert: true,
    // validateRoot: false,
  };
//   配置jwt
config.jwt = {
    secret:'twq2131fsaf123dd',
    expiresIn:60 * 60 * 24 * 3
};
// 跨域
config.cors = {
    origin: '*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
}
// 文件上传
config.multipart = {
    fileSize: '1mb',
    mode: 'file',
  };
//   阿里云对象存储oss
config.oss = {
    accessKeyId:'LTAI5tNhuPSs3QiAYcq8s1tE',
    accessKeySecret:'PvgA3dfExyhMsod0FBfmlnloU3KhVj',
    bucket:'chabaidao-app',
    region:'oss-cn-chengdu',
    folder:'naicha/'

}
  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  return {
    ...config,
    ...userConfig,
  };
};
